 /*
 * messlist:发送参数
 * msgnum：存放未读消息
 * numindex：判断是否有未读消息 count ： 存放消息数
 * db：数据库
 */
var messlist = {
	list: []
};
var msgnum = [];
var numindex = 0;
var count = 0; 
var db = openDatabase('messageChat', '1.0', 'messageChatOfHistory DB', 4 * 1024 * 1024);
var demo = new Vue({
	el: '#main',
	data: {
		messageList: []
	},
	methods: {
		openCircle: openCircle,
		clearM: clearM
	}
});

db.transaction(function(tx) {
	tx.executeSql('SELECT * FROM CurentOtherMessage', [], function(tx, results) {
		var len = results.rows.length,
			i;
		if (len > 0) {
			for (i = 0; i < len; i++) {
				//console.log(JSON.stringify(results.rows.item(i)));
				msgnum.push(results.rows.item(i).GuideId);
			}
			requstAjax(); //向后请求相关圈子信息
		}
	}, null);
});

function requstAjax() {
	if (msgnum.length > 0) {
		for (var i = 0; i < msgnum.length; i++) {
			var contid = msgnum[i];
			if (msgnum[i] != -1) {
				for (var j = 0; j < msgnum.length; j++) {
					if (contid == msgnum[j]) {
						count++;
						msgnum[j] = -1;
					}
				}
				if(count>99){ count=99+".."; }
				messlist.list[numindex] = {
					userId: contid,
					number: count
				};
				count = 0;
				numindex++;
			}
		}
	}
	if (numindex <= 0)
		return;
	var messageUrl = getUrl("62circleMessage");
	var headerJson = getHeader();
	var datas = "json=" + JSON.stringify(messlist);
	demo.$http({
		url: messageUrl,
		method: 'POST',
		headers: headerJson,
		data: datas
	}).then(function(response) {
		if (response.data.code == "0") {
			console.log(JSON.stringify(response.data.obj.list))
			demo.messageList = response.data.obj.list;
		} else {
			console.log("quanziMessage网络链接异常" + response.data.code)
		}
	}, function(response) {
		console.log('圈子分类错误' + JSON.stringify(response.data));
	});
}
//打开圈子
function openCircle(gid, cname, gpID, cId) {	
	localStorage.guideId = writeData(gid);
	localStorage.circleName = writeData(cname);
	localStorage.groupId = writeData(cId);
	localStorage.circleId = writeData(cId);
	document.getElementById(gpID).style.display = 'none';
	mui.openWindow({
		url: '../shopping/circleMain.html',
		id: 'circleMain.html',
		waiting: {
			autoShow: false
		},
		extras:{
			source:'quanziMessage'
		}
	})
}
//清空消息
function clearM() {
	var btnArray = ['否', '是'];
	mui.confirm('你确认要清除历史消息?', '提示', btnArray, function(e) {
		if (e.index == 1) {
			db.transaction(function(tx) {
				demo.messageList = [];
				//$scope.$apply();
				tx.executeSql('DELETE FROM CurentOtherMessage');
				mui.toast('清除历史消息成功');
				plus.webview.getWebviewById('home.html').reload(true);
			})
		}
	})
}